using BuildingBlocks.Presentation.Endpoints;
using BuildingBlocks.Presentation.Results;
using Mediator;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Routing;

namespace Catalog.Api.Features.CatalogItems.UpdateCatalogItem;

internal sealed class UpdateCatalogItemEndpoint : IEndpoint
{
    public void MapEndpoint(IEndpointRouteBuilder app)
    {
        app.MapPut(
                $"{Routes.Base}/{{id}}",
                [Authorize] async (
                    string id,
                    UpdateCatalogItemRequest request,
                    IMediator mediator,
                    CancellationToken cancellationToken) =>
                {
                    var command = new UpdateCatalogItemCommand(
                        id,
                        request.Name,
                        request.Description,
                        request.Price,
                        request.Stock,
                        request.PictureUrl,
                        request.Category);

                    var result = await mediator.Send(command, cancellationToken);
                    return result.Match(Results.NoContent, ApiResponse.Problem);
                })
            .WithName("UpdateCatalogItem")
            .WithTags("Catalog");
    }
}
